<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline" ref="ruleFormRef">
        <el-form-item label="字典名称" prop="dictName">
          <el-input
            v-model="queryParams.dictName"
            placeholder="请输入字典名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="getListPage"
          />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input
            v-model="queryParams.dictType"
            placeholder="请输入字典类型"
            clearable
            style="width: 240px"
            @keyup.enter.native="getListPage"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="字典状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 400px"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :teleported="false"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="getListPage"> 查询 </el-button>
            <el-button icon="Refresh" @click="reset(ruleFormRef)"> 重置 </el-button>
          </el-form-item>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <div class="util">
        <el-button
          type="primary"
          plain
          icon="CirclePlus"
          size="default"
          @click="addHandler"
          v-hasPermi="['system:menu:add']"
        >
          新增
        </el-button>

        <el-button
          type="danger"
          plain
          icon="Delete"
          size="default"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:edit']"
        >
          删除
        </el-button>

        <el-button
          type="danger"
          plain
          icon="Refresh"
          size="default"
          @click="handleRefreshCache"
          v-hasPermi="['system:dict:remove']"
        >
          刷新缓存
        </el-button>

        <el-button type="warning" plain icon="Download" size="default"> 导出 </el-button>
      </div>
      <div class="table-inner">
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="字典编号" align="center" prop="dictId" />
          <el-table-column
            label="字典名称"
            align="center"
            prop="dictName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
            <template #default="scope">
              <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">
                <span>{{ scope.row.dictType }}</span>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <DictTag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          ></el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                @click="editHandler(scope.row)"
                v-hasPermi="['system:dict:edit']"
              >
                修改
              </el-button>
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:dict:remove']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <!--分页器            -->
        <el-pagination
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 15, 20, 25, 30]"
          :background="true"
          layout="prev, pager, next, jumper, ->,  sizes, total"
          :total="total"
          @current-change="getListPage"
          @size-change="getListPage"
        />
      </div>
    </div>

    <TypeDialog ref="typeDialogRef"></TypeDialog>
  </div>
</template>

<script lang="ts" setup>
  import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
  import {
    reqDictTypeDeleteUrl,
    reqDictTypePageListUrl,
    reqRefreshCacheUrl,
  } from '@/api/system/dict'
  import { useDictStore } from '@/store/modules/dict'
  import { FormInstance } from 'element-plus'
  import DictTag from '@/components/DictTags/index.vue'
  import TypeDialog from './components/type-dialog.vue'

  const { proxy }: any = getCurrentInstance()
  const typeDialogRef = ref() /*对话框ref*/
  const ruleFormRef = ref()
  //是否展示搜索框
  const showSearch = ref(true)
  //遮罩层开关
  const loading = ref(true)
  // 选中数组
  const dictIds = ref([])
  // 非单个禁用
  const single = ref(true)
  // 非多个禁用
  const multiple = ref(true)
  //要展示的数据信息
  const tableData = ref()
  // 日期范围
  const dateRange = ref([])
  //总的数据
  const total = ref()
  //查询参数
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    dictName: '',
    dictType: '',
    status: '',
    beginTime: undefined,
    endTime: undefined,
  })

  //获取分页数据
  const getListPage = async () => {
    loading.value = true
    queryParams.beginTime = dateRange.value[0]
    queryParams.endTime = dateRange.value[1]
    loading.value = true
    let result = await reqDictTypePageListUrl(queryParams)
    if (result.code === 200) {
      //查询成功
      tableData.value = result.data
      total.value = result.total
      loading.value = false
    } else {
      proxy.$modal.msgError(result.message != null ? result.message : '获取分页数据失败！')
    }
    /*将这两个查询参数变成空*/
    queryParams.beginTime = undefined
    queryParams.endTime = undefined

    setTimeout(() => {
      loading.value = false
    }, 500)
  }

  // 重置
  const reset = (formEl: FormInstance | undefined) => {
    loading.value = true
    proxy.$resetForm(formEl)
    Object.keys(queryParams).forEach((key) => {
      if (key == 'pageSize') {
        queryParams[key] = 10
      } else if (key == 'pageNum') {
        queryParams[key] = 1
      } else {
        queryParams[key] = null
      }
    })
    dateRange.value = []
    /*再次查询*/
    getListPage()
    setTimeout(() => {
      loading.value = false
    }, 500)
  }

  //点击弹出添加或者修改对话框
  /*打开对话框进行添加操作*/
  const addHandler = () => {
    typeDialogRef.value.show()
  }

  /*修改操作*/
  const editHandler = (row: any = {}) => {
    typeDialogRef.value.show(row)
  }

  //删除按钮
  const handleDelete = (row: any = {}) => {
    const dict_ids = row.dictId || dictIds.value
    proxy.$modal
      .confirm('是否确认删除岗位编号为"' + dict_ids + '"的数据项？')
      .then(async () => {
        await reqDictTypeDeleteUrl(dict_ids).then(async (req: any) => {
          if (req.code == 200) {
            await getListPage()
            proxy.$modal.msgSuccess('删除成功')
          } else {
            proxy.$modal.msgError(req.message != null ? req.message : '删除成功')
          }
        })
      })
      .catch(() => {})
  }

  /*多选框*/
  const handleSelectionChange = (selection: any) => {
    dictIds.value = selection.map((item: any) => item.dictId)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  //导出数据
  const handleExport = () => {}

  //刷新缓存
  const handleRefreshCache = async () => {
    loading.value = true
    await reqRefreshCacheUrl().then(() => {
      proxy.$modal.msgSuccess('刷新成功')
      const dictStore = useDictStore()
      dictStore.cleanDict()
    })
    setTimeout(() => {
      loading.value = false
    }, 500)
  }

  onMounted(() => {
    getListPage() //获取第一页的信息
    setTimeout(() => {
      loading.value = false
    }, 500)
  })
</script>

<script lang="ts">
  export default {
    dicts: ['sys_normal_disable'],
  }
</script>

<style scoped lang="scss">
  .header {
    display: flex;
    padding: 16px 16px 0px 16px;
    margin-bottom: 16px;
    border-radius: 4px;
    background: white;
    box-shadow: 0 0 12px rgb(0 0 0 / 5%);
  }

  .footer {
    flex: 1;
    display: flex;
    padding: 16px;
    flex-direction: column;
    border-radius: 4px;
    overflow: hidden;
    background: white;
    box-shadow: 0 0 12px rgb(0 0 0 / 5%);
    position: relative;
    box-sizing: border-box;

    .util {
      margin-bottom: 15px;
      display: flex;
      justify-content: flex-end;
      flex-shrink: 0;
    }

    .table-inner {
      flex: 1;
      position: relative;
    }

    .table {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
    }
  }
</style>
